package com.jiyinit.web.core.mobile.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiyinit.core.framework.base.entity.ResponseDto;
import com.jiyinit.core.framework.base.paging.PagingResult;
import com.jiyinit.core.framework.base.util.Check;
import com.jiyinit.core.framework.base.util.JsonEntityTransform;
import com.jiyinit.web.core.mobile.dto.BlueWallCommentRequest;
import com.jiyinit.web.core.mobile.entity.TabCoreBlueWallCommentEntity;
import com.jiyinit.web.core.mobile.service.BlueWallCommentServiceImpl;

import java.util.Date;

@Controller
@RequestMapping(value = "mobile/blueWallComment")
public class BlueWallCommentController {
	
	private static Logger logger = LoggerFactory.getLogger(BlueWallCommentController.class);
	
	@Resource(name = "core.blueWallCommentServiceImpl")
	private BlueWallCommentServiceImpl service;
	
	
	/**
	 * 分页查询
	 * @param req
	 * @param request_json
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getPageRows")
	public PagingResult<TabCoreBlueWallCommentEntity> getPageRows(HttpServletRequest req, String request_json){
		logger.info("传入参数: {}", request_json);
		BlueWallCommentRequest request = JsonEntityTransform.json2Object(request_json, BlueWallCommentRequest.class);
		logger.info("转换为obj之后的参数: {}", request.toString());
		request.setLimit(999);
		PagingResult<TabCoreBlueWallCommentEntity> pag = service.findForPage(request);
		return pag;
	}

	/**
	 * 帖子评论详情
	 * @param req
	 * @param request_json
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getHistoryList")
	public ResponseEntity<ResponseDto> getHistoryList(HttpServletRequest req, String request_json){
		logger.info("传入参数: {}", request_json);
		BlueWallCommentRequest request = JsonEntityTransform.json2Object(request_json, BlueWallCommentRequest.class);
		logger.info("转换为obj之后的参数: {}", request.toString());

		PagingResult<TabCoreBlueWallCommentEntity> pag = service.historyList(request);
		return new ResponseEntity<ResponseDto>(ResponseDto.responseOK(pag), HttpStatus.OK);
	}
	
	/**
	 * 添加评论内容
	 * @param req
	 * @param request_json
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/toAdd", method=RequestMethod.POST)
	public ResponseEntity<ResponseDto> add(HttpServletRequest req, String request_json){
		logger.info("传入参数: {}", request_json);
		TabCoreBlueWallCommentEntity entity = JsonEntityTransform.json2Object(request_json, TabCoreBlueWallCommentEntity.class);
		logger.info("转换为obj之后的参数: {}", entity.toString());
		if(entity.getLevel() == 2 || entity.getLevel() == 3){
			if(entity.getToUserId()!= null){
				int i = 0;
				if(!Check.isNullObj(entity)){
					entity.setAttr1(String.valueOf(entity.getId()));
					entity.setCreateTime(new Date());
					entity.setIsDelete(0);
					i = service.add(entity);
					logger.info("添加之后 的json串:{}", JsonEntityTransform.object2Json(entity));
				}
				if (i>0) {
					return new ResponseEntity<>(ResponseDto.responseOK(""), HttpStatus.OK);
				}
				return new ResponseEntity<>(ResponseDto.responseFail("添加失败"),HttpStatus.OK);

			}else {
				int i = 0;
				if(!Check.isNullObj(entity)){
					entity.setCreateTime(new Date());
					entity.setIsDelete(0);
					entity.setAttr1(null);
					i = service.add(entity);
					logger.info("添加之后的json串:{}", JsonEntityTransform.object2Json(entity));
				}
				if (i>0) {
					return new ResponseEntity<>(ResponseDto.responseOK(""), HttpStatus.OK);
				}
				return new ResponseEntity<>(ResponseDto.responseFail("添加失败"),HttpStatus.OK);
			}
		}
    	return new ResponseEntity<>(ResponseDto.responseFail("权限不够，添加失败"),HttpStatus.OK);
	}
	
	/**
	 * 修改
	 * @param req
	 * @param request_json
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update", method=RequestMethod.POST)
	public ResponseEntity<ResponseDto> update(HttpServletRequest req, String request_json){
		logger.info("传入参数: {}", request_json);
		TabCoreBlueWallCommentEntity entity = JsonEntityTransform.json2Object(request_json, TabCoreBlueWallCommentEntity.class);
		logger.info("转换为obj之后的参数: {}", entity.toString());
		int i = 0;
		if(!Check.isNullObj(entity)){
			i = service.update(entity);
			logger.info("更新之后的json串:{}", JsonEntityTransform.object2Json(entity));
		}
		if (i>0) {
    		return new ResponseEntity<>(ResponseDto.responseOK(""), HttpStatus.OK);
    	}
    	return new ResponseEntity<>(ResponseDto.responseFail(""),HttpStatus.OK);
	}
	
	/**
	 * 删除
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete", method=RequestMethod.POST)
	public ResponseEntity<ResponseDto> delete(HttpServletRequest req, String request_json) {
		logger.info("传入参数: {}", request_json);
		String str = req.getParameter("ids");
		int i = 0;
		if(!Check.isNullStrStrict(str)){
			String[] strs = str.split(",");
			for (int j = 0; j < strs.length; j++) {
				int id = Integer.valueOf(strs[i]);
				i += service.delete(id);
			}
		}
		
		if (i>0) {
    		return new ResponseEntity<>(ResponseDto.responseOK(""), HttpStatus.OK);
    	}
    	return new ResponseEntity<>(ResponseDto.responseFail(""),HttpStatus.OK);
	}

}
